package com.github.icloudpay.crm.core.biz;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.icloudpay.crm.core.entity.Consumer;
import com.github.icloudpay.crm.core.entity.PersonalBindCard;
import com.github.icloudpay.crm.core.mapper.ConsumerMapper;
import com.github.icloudpay.crm.core.mapper.PersonalBindCardMapper;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;

/**
 * 用户绑卡表
 *
 * @author centerroot
 * @email lfw6699@163.com
 * @date 2018-05-25 16:28:16
 */
@Service
public class PersonalBindCardBiz extends BaseBiz<PersonalBindCardMapper,PersonalBindCard> {
	private static final Logger logger = LoggerFactory.getLogger(PersonalBindCardBiz.class);

	@Autowired
	private PersonalBindCardMapper personalBindCardMapper;
	@Autowired
	private ConsumerMapper consumerMapper;
	/**
	 * 添加用户银行卡信息
	 * @author centerroot
	 * @time 创建时间:2018年5月21日下午1:39:55
	 * @param param
	 * @return
	 */
	public Map<String, Object> savePersonalBindCardInfo(Map<String, Object> param){
		logger.info("添加用户银行卡信息 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String consumerId = (String) param.get("consumerId");
		Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
		if (null != consumer && null != consumer.getPerId()) {
			
			PersonalBindCard personalBindCard = new PersonalBindCard();
			String seqNo = String.valueOf(SnowFlake.getId());
			personalBindCard.setSeqNo(seqNo);
			personalBindCard.setPerId(consumer.getPerId());
			personalBindCard.setPayeeName((String) param.get("realName"));
			String bindEncryptCardNo = (String) param.get("bindEncryptCardNo");
			personalBindCard.setBindEncryptCardNo(bindEncryptCardNo);
			String startCardNo = bindEncryptCardNo.substring(0, 4);
			String endCardNo = bindEncryptCardNo.substring(bindEncryptCardNo.length()-4, bindEncryptCardNo.length());
			personalBindCard.setBindCardNo(startCardNo + " **** **** " + endCardNo);
			personalBindCard.setBankName((String) param.get("bankName"));
			personalBindCard.setSubbranchName((String) param.get("subbranchName"));
			personalBindCard.setCreatetime(new Timestamp(System.currentTimeMillis()));
			personalBindCardMapper.insert(personalBindCard);
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}else{
			respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
		}
		logger.info("添加用户银行卡信息 响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 删除用户银行卡信息
	 * @author centerroot
	 * @time 创建时间:2018年5月21日下午1:39:55
	 * @param param
	 * @return
	 */
	public Map<String, Object> deletePersonalBindCardInfo(Map<String, Object> param){
		logger.info("添加用户银行卡信息 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		
		try {
			String seqNo = (String) respMap.get("seqNo");
			personalBindCardMapper.deleteByPrimaryKey(seqNo);
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}catch(Exception e){
			e.printStackTrace();
			respMap.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			respMap.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
		}
		
		logger.info("添加用户银行卡信息 响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 查询用户银行卡信息
	 * @author centerroot
	 * @time 创建时间:2018年5月21日下午1:39:55
	 * @param param
	 * @return
	 */
	public Map<String, Object> getPersonalBindCardInfoList(Map<String, Object> param){
		logger.info("添加用户银行卡信息 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String userNo = (String) param.get("userNo");
		Consumer consumer = consumerMapper.selectByPrimaryKey(userNo);
		if (null != consumer && null != consumer.getPerId()) {
			PersonalBindCard personalBindCard = new PersonalBindCard();
			personalBindCard.setPerId(consumer.getPerId());
			List<PersonalBindCard> custBindCards = personalBindCardMapper.select(personalBindCard);
			respMap.put("custBindCards", custBindCards);
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}else{
			respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
		}
		
		logger.info("添加用户银行卡信息 响应参数：{}",respMap);
		return respMap;
	}
}